package com.tingyukeji.fastpay;

import android.databinding.DataBindingUtil;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.RadioGroup;

import com.tingyukeji.fastpay.base.BaseActivity;
import com.tingyukeji.fastpay.databinding.ActivityMainBinding;
import com.tingyukeji.fastpay.ui.fragment.CircleFragment;
import com.tingyukeji.fastpay.ui.fragment.MyFragment;
import com.tingyukeji.fastpay.ui.fragment.CenterFragment;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    private ActivityMainBinding binding;
    public FragmentManager fragmentManager;
    private CenterFragment centerFragment;
    private CircleFragment circleFragment;
    private MyFragment myFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setMainActivity(this);
        binding.rgTabs.setOnCheckedChangeListener(this);
        binding.rgTabs.check(R.id.rb_center);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideAllFragment(transaction);
        switch (checkedId) {
            case R.id.rb_center:
                if (null == centerFragment) {
                    centerFragment = new CenterFragment();
                    transaction.add(R.id.base_id, centerFragment);
                } else
                    transaction.show(centerFragment);
                break;
            case R.id.rb_circle:
                if (null == circleFragment) {
                    circleFragment = new CircleFragment();
                    transaction.add(R.id.base_id, circleFragment);
                } else {
                    transaction.show(circleFragment);
                }
                break;
            case R.id.rb_my:
                if (null == myFragment) {
                    myFragment = new MyFragment();
                    transaction.add(R.id.base_id, myFragment);
                } else
                    transaction.show(myFragment);
                break;
        }
        if (!isDestroyed()) transaction.commitAllowingStateLoss();
    }

    private void hideAllFragment(FragmentTransaction transaction) {
        if (circleFragment != null)
            transaction.hide(circleFragment);
        if (centerFragment != null)
            transaction.hide(centerFragment);
        if (null != myFragment)
            transaction.hide(myFragment);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
